package com.web.action;

import com.util.BaseAction;
import com.web.domain.Hotel;
import com.web.domain.Order;
import com.web.domain.User;
import com.web.service.HotelService;
import com.web.service.OrderService;
import com.web.service.UserService;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * Created by 陈国钰 on 2016/9/9.
 */
@Controller
@Scope("prototype")
@ParentPackage(value = "struts-default")
@Namespace(value = "/")
@Action(value = "orderAction", results = {
        @Result(name = "list", location = "/order/order_list.jsp"),
        @Result(name = "order", location = "/order/show_order.jsp"),
        @Result(name = "comment", location = "/order/comment.jsp"),
        @Result(name = "showcomment", location = "/order/showcomment.jsp")
})
public class OrderAction extends BaseAction {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private HotelService hotelService;
    private Order order;
    private int orderId;
    private List<Order> orders;
    private User user;
    private Hotel hotel;
    private String comment;

    public String booking() {
        orderService.booking(order);
        return null;
    }

    public String cancleOrder() {
        orderService.cancleOrder(orderId);
        User user = (User) session.getAttribute("user");
        orders = orderService.orderList(user);
        return "list";
    }

    public String comment() {
        Order order1 = orderService.findOrder(orderId);
        order1.setComment(comment);
        orderService.comment(order1);
        User user = (User) session.getAttribute("user");
        orders = orderService.orderList(user);
        return "list";
    }

    public String showCommentView() {
        order = orderService.findOrder(orderId);
        user = userService.findUserById(order.getUserId());
        hotel = hotelService.findHotelById(order.getHotelId());
        return "comment";
    }

    public String showComment() {
        order = orderService.findOrder(orderId);
        return "showcomment";
    }

    public String canclePayOrder() {
        orderService.canclePayOrder(orderId);
        User user = (User) session.getAttribute("user");
        orders = orderService.orderList(user);
        return "list";
    }

    public String orderList() {
        User user = (User) session.getAttribute("user");
        orders = orderService.orderList(user);
        return "list";
    }

    public String searchOrder() {
        order = orderService.findOrder(orderId);
        user = userService.findUserById(order.getUserId());
        hotel = hotelService.findHotelById(order.getHotelId());
        return "order";
    }

    public String pay() {
        orderService.pay(orderId);
        User user = (User) session.getAttribute("user");
        orders = orderService.orderList(user);
        return "list";
    }

    public String delete() {
        orderService.delete(orderId);
        User user = (User) session.getAttribute("user");
        orders = orderService.orderList(user);
        orderList();
        return "list";
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public List<Order> getOrders() {
        return orders;
    }

    public void setOrders(List<Order> orders) {
        this.orders = orders;
    }

    public int getOrderId() {
        return orderId;
    }

    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Hotel getHotel() {
        return hotel;
    }

    public void setHotel(Hotel hotel) {
        this.hotel = hotel;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }


    public String execute() throws Exception {
        return null;
    }
}
